﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;
using System.Xml;
using log4net;
using System.Reflection;
using ZLCMS.Web.PayChannal;
using System.Collections;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Web.SystemManage.Basics.ProductSKU;
using ZLCMS.Business.Shop.Data;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class SkuOrderSendGoods : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected ShopOrderInfo _orderDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            //if (IsPostBack)
            //{
            //    Save();
            //}


        }



        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    IList<ShopShippingInfo> list = session.CreateCriteria<ShopShippingInfo>().List<ShopShippingInfo>();
                    list.Insert(0, new ShopShippingInfo() { Type ="-1", ShippingName = "--请选择--" });
                    ddl_shippinglist.DataSource = list;
                }
                ddl_shippinglist.DataTextField = "ShippingName";
                ddl_shippinglist.DataValueField = "Type";
                ddl_shippinglist.DataBind();
            }
        }

        protected void InitializeGlobalValue()
        {
            string oid;

            try
            {
                if (!IsPostBack)
                {
                    oid = Request["uid"] == null ? "" : Request["uid"];
                    _oid.Text = oid;
                }
                else
                {
                    oid = _oid.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _orderDb = session.CreateCriteria<ShopOrderInfo>().Add(Expression.Eq("OrderNo", oid)).UniqueResult<ShopOrderInfo>();

                    IList<ShopOrderProduct> list = session.CreateCriteria<ShopOrderProduct>().Add(Expression.Eq("OrderNo", oid)).List<ShopOrderProduct>();
                    
                    //设置sku属性
                    foreach (ShopOrderProduct item in list)
                    {
                        string paramCode = session.CreateSQLQuery("select PARAM_CODE from shop_sku_product where PRODUCT_SKU_CODE=:sku")
                            .SetParameter("sku", item.SkuProductNo)
                            .UniqueResult<string>();
                        string str = "";
                        List<SKUGroup> groups = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SKUGroup>>(paramCode);
                        if (groups != null)
                        {
                            foreach (var sku in groups)
                            {
                                string itemName = session.CreateSQLQuery("select ITEM_NAME from shop_sku_item where RECID=" + sku.ItemId)
                                    .UniqueResult<string>();
                                str += "【" + itemName + "】";
                            }
                        }
                        //return str;
                        item.ProductName = item.ProductName + str;
                    }
                    
                    list.Insert(0, new ShopOrderProduct() { SkuProductNo = "-1", ProductName = "--请选择--" });

                    ddl_productlist.DataSource = list;
                }
                ddl_productlist.DataTextField = "ProductName";
                ddl_productlist.DataValueField = "SkuProductNo";
                ddl_productlist.DataBind();
                if (_orderDb == null)
                {
                    throw new Exception("找不到对应参数信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        //protected void Save()
        //{
        //    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
        //    {
        //        try
        //        {
        //            if (!PayChannalKit.SendGoodsConfirm(session, _orderDb))
        //            {
        //                throw new Exception("调用发货接口错误");
        //            }

        //            _orderDb.ShippingStatus = "1";
        //            _orderDb.ShippingNo = tb_shippingno.Text;
        //            _orderDb.ShippingType = shippinglist.SelectedValue;
        //            _orderDb.Shiptime = DateTime.Now;
        //            _orderDb.ManageHistory += string.Format("<br><font color=red>【{0}】确认发货，({1})</font><br>{2}", UserKit.GetUserAccount(), DateTime.Now, tb_memo.Text);

        //            session.Update(_orderDb);
        //            session.Flush();
        //        }
        //        catch (System.Exception ex)
        //        {
        //            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
        //            return;
        //        }
        //    }

        //    // 刷新 editparamgroup 页面
        //    _framePage.Alert(new ReturnCode()
        //    {
        //        statusCode = 200,
        //        message = "操作成功!",
        //        callbackType = "closeCurrent",
        //        navTabId = "vieworder"
        //    });
        //}
    }
}
